package com.lutzyt.delivery.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutzyt.delivery.bean.OrderItem;
import com.lutzyt.delivery.service.OrderItemService;
import com.lutzyt.delivery.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller 控制器
 *
 * @author zyt
 * @date 2021-06-17
 */
@Slf4j
@RestController
@RequestMapping("/api/order/item")
public class OrderItemController {
  private OrderItemService service;

  @GetMapping("/{id}")
  public Result<OrderItem> getById(@PathVariable Integer id) {
    final OrderItem bean = service.getById(id);
    return Result.success(bean);
  }

  @PutMapping("/{id}")
  public Result<Void> update(@RequestBody OrderItem bean) {
    log.info("更新:{}", bean);
    service.update(bean);
    return Result.success();
  }

  @PostMapping("/page")
  public Result<Page<OrderItem>> page(Integer pageSize, Integer pageNum, @RequestParam("id") Integer orderId) {
    final Page<OrderItem> page = service.findByPage(pageNum, pageSize, orderId);
    return Result.success(page);
  }

  @DeleteMapping("/{id}")
  public Result<Void> del(@PathVariable Integer id) {
    log.info("删除:{}", id);
    service.deleteById(id);
    return Result.success();
  }

  @PostMapping
  public Result<Void> insert(@RequestBody OrderItem bean) {
    log.info("添加:{}", bean);
    service.insert(bean);
    return Result.success();
  }

  @Autowired
  public void setService(OrderItemService service) {
    this.service = service;
  }
}
    